package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.SurgeryManage;
import com.woniuxy.entity.Tde;
import com.woniuxy.mapper.SurgeryManageMapper;
import com.woniuxy.service.SaasService;
import com.woniuxy.service.SurgeryManageService;
import com.woniuxy.util.CONSTANT;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import net.sf.jsqlparser.expression.operators.arithmetic.Concat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.data.redis.core.RedisTemplate;
import com.woniuxy.util.ResponseEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SurgeryManageServiceImpl implements SurgeryManageService, SaasService {

    @Resource
    private SurgeryManageMapper surgeryManageMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public ResponseData<?> querySurgery(PageParam<SurgeryManage> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<SurgeryManage> surgeryManageList = surgeryManageMapper.queryList(pageParam.getParam());
        return new ResponseData<>().ok(new PageInfo<>(surgeryManageList));
    }

    @Override
    public ResponseData<?> addSurgery(SurgeryManage surgeryManage) {


        if (StringUtils.isBlank(surgeryManage.getSiName())||surgeryManage.getSiPrice()==null||
            StringUtils.isBlank(String.valueOf(surgeryManage.getSiPrice()))||surgeryManage.getSiDepartmentId()==null||
            StringUtils.isBlank(String.valueOf(surgeryManage.getSiPrice()))){
            return new ResponseData<>().fail(ResponseEnum.MANDATORY_EMPTY);
        }
        surgeryManageMapper.addSurface(surgeryManage);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> querySurgeryById(Integer id) {
        SurgeryManage surgeryManage = surgeryManageMapper.querySurfaceById(id);
        return new ResponseData<>().ok(surgeryManage);
    }

    @Override
    public ResponseData<?> updateSurgery(SurgeryManage surgeryManage) {
        if (StringUtils.isBlank(surgeryManage.getSiName())||surgeryManage.getSiPrice()==null||
                StringUtils.isBlank(String.valueOf(surgeryManage.getSiPrice()))||surgeryManage.getSiDepartmentId()==null||
                StringUtils.isBlank(String.valueOf(surgeryManage.getSiPrice()))){
            return new ResponseData<>().fail(ResponseEnum.MANDATORY_EMPTY);
        }
        surgeryManageMapper.updateSurface(surgeryManage);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> deleteSurgery(Integer id) {
        SurgeryManage surgeryManage = new SurgeryManage();
        surgeryManage.setId(id);
        surgeryManage.setSiStatus("2");
        surgeryManageMapper.updateSurface(surgeryManage);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> queryByDeList(Tde tde) {
        List<SurgeryManage> surgeryManages = (List<SurgeryManage>) redisTemplate.opsForHash().get(CONSTANT.SURGERY_MANAGE, ""+tde.getId());
        if (surgeryManages == null) {
            surgeryManages = surgeryManageMapper.queryByDeList(tde);
            redisTemplate.opsForHash().put(CONSTANT.SURGERY_MANAGE, ""+tde.getId(), surgeryManages);
        }
        return new ResponseData<>().ok(surgeryManages);
    }
}
